package com.maxjay.system.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.maxjay.system.dao.UserDaoJdbc;

@SuppressWarnings({"rawtypes", "unchecked"})
@Repository
public class UserDaoJdbcImpl extends BaseDaoJdbc implements UserDaoJdbc {

	private static Logger logger = Logger.getLogger(UserDaoJdbcImpl.class);
	
	public Set<String> getUserFunctions(String userId) {
		String sql = "select FUNCTION_ID from VW_USER_FUNCTION where USER_ID = ?";
		logger.debug("查询用户[USER_ID:" + userId + "]权限的SQL为：" + sql);
		Set<String> userFunctions = (Set<String>)this.getJdbcTemplate().query(sql, new Object[]{ userId }, new ResultSetExtractor() {
			Set<String> functions = new HashSet<String>();
			public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
				while(rs.next()){
					functions.add(rs.getString(1));
				}
				return functions;
			}
		});
		return userFunctions;
	}

	public Set<String> getUserRoles(String userId) {
		String sql = "select ROLE_ID from T_SYS_USER_ROLE where USER_ID = ?";
		logger.debug("查询用户[USER_ID:" + userId + "]角色的SQL为：" + sql);
		Set<String> userRoles = (Set<String>)this.getJdbcTemplate().query(sql, 
				new Object[]{ userId }, new ResultSetExtractor() {
			Set<String> roles = new HashSet<String>();
			public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
				while(rs.next()){
					roles.add(rs.getString(1));
				}
				return roles;
			}
		});
		return userRoles;
	}
}
